package com.ctgu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ctgu.common.ResponseResult;
import com.ctgu.model.entity.*;
import com.ctgu.mapper.DepartmentMapper;
import com.ctgu.service.ICompanyService;
import com.ctgu.service.IDepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctgu.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.io.Console;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yiMa
 * @since 2023-07-05
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {
    @Lazy
    @Autowired
    private IEmployeeService employeeService;
    @Lazy
    @Autowired
    private ICompanyService companyService;
    @Override
    public ResponseResult getList() {
        LoginUser loginUser=(LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        LambdaQueryWrapper<Department> wrapper=new LambdaQueryWrapper<>();
        if (loginUser.getUser().getRoleId().equals("4")){
            wrapper.eq(Department::getCompanyId,loginUser.getUser().getId());
        }
        else if (loginUser.getUser().getRoleId().equals("1")){
            return new ResponseResult(400,"超级管理员无法获取部门");
        }
        else{
            Employee employee=(Employee) loginUser.getUser();
            wrapper.eq(Department::getCompanyId,employee.getCompanyId());
        }
        List<Department> departmentList=list(wrapper);
        return new ResponseResult(200,"获取公司部门成功",departmentList);
    }

    @Override
    public ResponseResult add(Department department) {

        LoginUser loginUser=(LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        //获取公司id
        Integer company_id = loginUser.getUser().getId();
        LambdaQueryWrapper<Department> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.eq(Department::getName,department.getName());
        queryWrapper.eq(Department::getCompanyId,company_id);
        Department one = getOne(queryWrapper);
        if(Objects.nonNull(one)){
            return new ResponseResult<>(400,"该部门已经存在请重新输入");
        }
        department.setCompanyId(company_id);
        department.setEnabled(true);
        save(department);
        return new ResponseResult(200,"添加部门成功",company_id);
    }

    @Override
    public ResponseResult delete(int id) {
        ResponseResult list = employeeService.getList(1, 1, null, id);
        Page page = (Page) list.getData();
        LambdaQueryWrapper<Department> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.eq(Department::getId,id);
        if(page.getRecords().isEmpty()){
            remove(queryWrapper);
            return new ResponseResult(200,"删除部门成功");
        }
        return new ResponseResult(400,"该部门存在员工，请移出员工再进行删除");
    }

    @Override
    public ResponseResult updateInfo(Department department) {
        LoginUser loginUser=(LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        int company_id=loginUser.getUser().getId();
        LambdaQueryWrapper<Department> queryWrapper=new LambdaQueryWrapper();
        //查询当前公司有无重复部门
        queryWrapper.eq(Department::getName,department.getName());
        queryWrapper.eq(Department::getCompanyId,company_id);
        Department one = getOne(queryWrapper);
        if(Objects.nonNull(one)){
            return new ResponseResult<>(400,"该部门已经存在请重新输入");
        }
        LambdaQueryWrapper<Department> queryWrapper1=new LambdaQueryWrapper();
        queryWrapper1.eq(Department::getId,department.getId());
        Department department1 = getOne(queryWrapper1);
        final CopyOptions copyOptions = CopyOptions.create();
        copyOptions.setOverride(false);
        copyOptions.setIgnoreNullValue(true);
        BeanUtil.copyProperties(department1,department,copyOptions);
        updateById(department);
        return new ResponseResult<>(200,"更新成功",department);
    }
}
